{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "491fd4bf",
   "metadata": {},
   "source": [
    "------\n",
    "* 2022.11.21/week14\n",
    "* Python\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e57ad3e",
   "metadata": {},
   "source": [
    "# 本周目标\n",
    "> 1、函数的定义  \n",
    "> 2、如何使用函数  \n",
    "> 3、函数的返回值  \n",
    "> 4、使用注释改进文档  \n",
    "> 5、传递任意数量的实参  \n",
    "> 6、函数与模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "daa8e4e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello \"+username+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "676c496c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello doggie~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"doggie\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a67d06f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    \"\"\"y=2x的函数运算\"\"\"\n",
    "    print(2*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2f4ad762",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "f(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "407fcf8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 编程思维(计算思维)下，函数的应用\n",
    "def nfu_news_url(类别,page):\n",
    "    for page in range(page):\n",
    "        print(\"https://www.nfu.edu.cn/\" + 类别 + \"/index\" + str(page) +\".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d8ea71ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/tzgg/index0.htm\n",
      "https://www.nfu.edu.cn/tzgg/index1.htm\n",
      "https://www.nfu.edu.cn/tzgg/index2.htm\n",
      "https://www.nfu.edu.cn/tzgg/index3.htm\n",
      "https://www.nfu.edu.cn/tzgg/index4.htm\n",
      "https://www.nfu.edu.cn/tzgg/index5.htm\n",
      "https://www.nfu.edu.cn/tzgg/index6.htm\n",
      "https://www.nfu.edu.cn/tzgg/index7.htm\n",
      "https://www.nfu.edu.cn/tzgg/index8.htm\n",
      "https://www.nfu.edu.cn/tzgg/index9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"tzgg\",10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcecaf94",
   "metadata": {},
   "source": [
    "## 2.如何使用函数（传递实参）\n",
    "> 1.positional argument（位置参数）  \n",
    "> 2.keyword argument（关键字参数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c7f99e32",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过案例来思考：位置参数 VS 关键字参数\n",
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9a6f2ea8",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3059410298.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [29]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4c0d120e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9e95fee9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0cd32aa0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1ad360fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "520df84c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "f(2,3,5) # 根据上面的经验，这个应该是位置参数吧（领悟）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "910a704a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "f(y=10,x=3,z=7) # 这个应该是关键字参数吧（领悟），关键字参数考虑顺序嘛？（不考虑顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9c19d7bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9) # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "c325c24c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=8) #"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "0fdd1733",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (1702135503.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [41]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(3,y=9,10) #\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(3,y=9,10) # 关键参数是不可以写在位置参数前面的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8850cd6a",
   "metadata": {},
   "source": [
    "> 3.默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4905e70a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n",
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息，如果你的宠物是dog，可以不选择填写animal_type\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "    \n",
    "describe_pet('harry','hamster')\n",
    "describe_pet('python','cat')\n",
    "describe_pet('Jerry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "78a1d21f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world 你好 世界\n",
      "hello world 你好 世界\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",\"world\",\"你好\",\"世界\")\n",
    "print(\"hello\",\"world\",\"你好\",\"世界\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "1f75858b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3794b633",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3cb71d5b",
   "metadata": {},
   "source": [
    "## 3.函数的返回值\n",
    "> 1.几乎所有编程语言，函数是要求必须有返回值  \n",
    "> 2.Python作为高级语言，可以不写返回值，但是一般情况下，也是要写的，不写的话，默认返回None  \n",
    "> 3. *函数的返回值*：return\n",
    ">> 3.1 return 可以返回过程运算的结果  \n",
    ">> 3.2 return 可以返回过程自定义结果（由程序开发人员/设计人员定制的）\n",
    "\n",
    "## 4.函数的优势\n",
    "* 1. 可重复使用\n",
    "* 2. 具有强大的可扩展性\n",
    "* 3. 具有可定制化性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5efcbc1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ad809d62",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = f(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "632a7f80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed250483",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
